<!-- eslint-disable no-unused-vars -->
<template>
  <div class="emplyee-container">
    <div class="left">
      <el-input placeholder="请输入内容" prefix-icon="el-icon-search" />

      <el-tree
        ref="treeRef"
        :data="treeList"
        :expand-on-click-node="false"
        :props="defaultProps"
        default-expand-all
        highlight-current
        node-key="id"
        show-checkbox
        @current-change="currentChange"
      />
    </div>
    <div class="right">
      <div class="r-top">
        <el-button size="mini" type="primary">添加员工</el-button>
        <el-button size="mini">excel导入</el-button>
        <el-button size="mini">excel导出</el-button>
      </div>
      <div class="r-middle">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="头像" prop="staffPhoto" width="180">
            <template v-slot="{ row }">
              <img
                v-if="row.staffPhoto"
                :src="row.staffPhoto"
                alt
                class="pic-staffPhoto"
              >
              <span v-else class="pic-tou">
                {{ row.username && row.username[0].charAt() }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" width="180" />
          <el-table-column label="手机号" prop="mobile" sortable />
          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <span slot-scope="{ row }">
              {{ row.formOfEmployment === 1 ? "正式" : "非正式" }}
            </span>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职日期" prop="timeOfEntry" sortable />
          <el-table-column label="操作">
            <template>
              <el-button type="text">查看</el-button>
              <el-button type="text">角色</el-button>
              <el-button type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="r-bottom">
        <el-pagination
          :current-page="pageParams.page"
          :page-size="pageParams.pagesize"
          :total="total"
          layout="total, sizes, prev, pager, next"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getDepartmentRequest } from '@/api/department'
import { transfromListToTree } from '@/utils'
import { getEmployeeRequest } from '@/api/employee'
// eslint-disable-next-line no-unused-vars
import _ from 'lodash'
export default {
  name: 'EmployeeCom',
  data() {
    return {
      tableData: [],
      treeList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      total: 0,
      // 后面发请求需要的一些参数，一般包在一个对象里面，不要写散了
      pageParams: {
        page: 1,
        pagesize: 10,
        keyword: '',
        departmentid: ''
      }
    }
  },
  created() {
    this.getDepartment()
    this.getEmployee()
  },
  methods: {
    async getDepartment() {
      const res = await getDepartmentRequest()
      // 下面data数据，是给el-tree组件使用的 树形结构
      // 下面这句话，是将treeList数据更新，然后就会触发dom更新，但是dom更新是异步的
      this.treeList = transfromListToTree(res.data, 0)

      this.pageParams.departmentid = this.treeList[0].id

      // 下面代码就是等dom更新以后，再去设置dom的一些变化
      this.$nextTick(() => {
        this.$refs.treeRef.setCurrentKey(this.pageParams.departmentid)
      })
    },
    currentChange(a, b) {
      console.log(a, b)
      this.pageParams.departmentid = a.id
      this.getEmployee()
    },
    async getEmployee() {
      const res = await getEmployeeRequest(this.pageParams)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.pageParams.pagesize = val
      this.getEmployee()
    },
    handleCurrentChange(val) {
      this.pageParams.page = val
      this.getEmployee()
    }
  }
}
</script>

<style lang="scss" scoped>
.emplyee-container {
  padding: 20px;
  display: flex;
  .left {
    padding: 20px;
    // background-color: lightcoral;
    flex: 0 1 280px;
  }
  .right {
    padding: 20px;
    // background-color: lightblue;
    flex: 1;
    .r-top {
      text-align: right;
    }
  }
  .r-middle {
    margin: 15px 0;
    .pic-staffPhoto {
      width: 40px;
      height: 40px;
    }
    .pic-tou {
      display: block;
      width: 40px;
      height: 40px;
      border-radius: 20px;
      background-color: orange;
      color: purple;
      font-size: 16px;
      text-align: center;
      line-height: 40px;
    }
  }
  .r-bottom {
    text-align: right;
  }
}
</style>
